//File: RegisterUserView.java

package view;

import java.sql.SQLException;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import model.Flight;
import model.QueryModel;

/**
 * Creates a view to register a user.
 *
 * @author Tobias Robinson
 */
public class SearchFlightView extends JDialog implements ActionListener {

    private ArrayList<String> origins = null;
    private Object[] originItems = null;
    private ArrayList<String> destinations = null;
    private Object[] destinationItems = null;
    private ArrayList<String> dates = null;
    private Object[] dateItems = null;
    private JList originList = null;
    private JList destinationList = null;
    private JList dateList = null;
    private JComboBox originCombo = null;
    private JComboBox destinationCombo = null;
    private JComboBox dateCombo = null;
    private QueryModel query = null;

    //input to pass to UCRegisterUser
    private String origin = null;
    private String destination = null;
    private String date = null;

    //components of the login window
    private JLabel originLabel = new JLabel("Select origin:  ");
    private JLabel destinationLabel = new JLabel("Select destination:  ");
    private JLabel dateLabel = new JLabel("Select date:  ");
    private JButton searchButton = new JButton("Search");

    /**
     * Creates a view to register user.
     *
     * @param parent tells view to run over main window
     */
    public SearchFlightView(JFrame parent) throws SQLException {
        super(parent, "Search Flight", true);
        setResizable(true);

        //content pane for login window
        JPanel registerPane = new JPanel();
        setContentPane(registerPane);

        //user GridBag layout
        GridBagLayout gb = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        registerPane.setLayout(gb);
        registerPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 11, 11));

        // create origin combo box
        origins = Flight.getAirports();
        originItems = origins.toArray();
        originCombo = new JComboBox(originItems);
        originCombo.setEditable(false);
        // create a list with the same data model
        originList = new JList(originCombo.getModel());

        // create destination combo box
        destinations = Flight.getAirports();
        destinationItems = destinations.toArray();
        destinationCombo = new JComboBox(destinationItems);
        destinationCombo.setEditable(false);
        // create a list with the same data model
        destinationList = new JList(destinationCombo.getModel());

        // create dates combo box
        dates = Flight.getFlightDates();
        dateItems = dates.toArray();
        dateCombo = new JComboBox(dateItems);
        dateCombo.setEditable(false);
        // create a list with the same data model
        dateList = new JList(dateCombo.getModel());

        // place the origin label
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.insets = new Insets(10, 0, 10, 0);
        c.anchor = GridBagConstraints.WEST;
        gb.setConstraints(originLabel, c);
        registerPane.add(originLabel);

        //add origin combo box to panel
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 0, 5, 10);
        c.anchor = GridBagConstraints.EAST;
        gb.setConstraints(originCombo, c);
        registerPane.add(originCombo);

        // place destination label
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.insets = new Insets(10, 0, 10, 0);
        c.anchor = GridBagConstraints.WEST;
        gb.setConstraints(destinationLabel, c);
        registerPane.add(destinationLabel);

        //add destination combo box to panel
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 0, 5, 10);
        c.anchor = GridBagConstraints.EAST;
        gb.setConstraints(destinationCombo, c);
        registerPane.add(destinationCombo);

        // place the date label
        c.gridwidth = GridBagConstraints.RELATIVE;
        c.insets = new Insets(10, 10, 5, 0);
        c.anchor = GridBagConstraints.WEST;
        gb.setConstraints(dateLabel, c);
        registerPane.add(dateLabel);

        //add destination combo box to panel
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 0, 5, 10);
        c.anchor = GridBagConstraints.EAST;
        gb.setConstraints(dateCombo, c);
        registerPane.add(dateCombo);
        
        // place the create button
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.insets = new Insets(10, 10, 5, 10);
        c.anchor = GridBagConstraints.CENTER;
        gb.setConstraints(searchButton, c);
        registerPane.add(searchButton);

        // end of layout

        // Register password field and OK button with action event handler.
        // An action event is generated when the return key is pressed while
        // the cursor is in the password field or when the OK button is pressed.
        searchButton.addActionListener(this);
    }

    /**
     * When user click button or presses enter processes information.
     *
     * @param e event type
     */
    public void actionPerformed(ActionEvent e) {
            origin = originCombo.getSelectedItem().toString();
            destination = destinationCombo.getSelectedItem().toString();
            date = dateCombo.getSelectedItem().toString();
            Flight.searchFlight(origin, destination, date);

            dispose();

    }
}